package cn.tedu.hospitalgroupproject.server.content.service.impl;

import cn.tedu.hospitalgroupproject.server.common.ex.ServiceException;
import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.ServiceCode;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDiagnosisReportRepository;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.DiagnosisReport;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Doctor;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DiagnosisReportAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DiagnosisReportUpdateParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorUpdateInfoParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.AppointmentListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DiagnosisReportListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DiagnosisReportStandardVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorStandardVO;
import cn.tedu.hospitalgroupproject.server.content.service.IDiagnosisReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DiagnosisReportServiceImpl implements IDiagnosisReportService {

    @Value("${hospital-appointment.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IDiagnosisReportRepository diagnosisReportRepository;

    public DiagnosisReportServiceImpl() {
        log.info("创建业务对象:DiagnosisReportServiceImpl");
    }

    @Override
    public void addNew(DiagnosisReportAddNewParam diagnosisReportAddNewParam) {
        log.debug("开始处理[新增诊断报告]的业务,参数:{}",diagnosisReportAddNewParam);
        DiagnosisReport diagnosisReport=new DiagnosisReport();
        BeanUtils.copyProperties(diagnosisReportAddNewParam,diagnosisReport);

        diagnosisReportRepository.insert(diagnosisReport);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除诊断报告]的业务,报告id:{}",id);
        //执行删除
        diagnosisReportRepository.deleteById(id);
    }

    @Override
    public void updateInfoById(DiagnosisReportUpdateParam diagnosisReportUpdateParam) {
        log.debug("开始处理【修改诊断报告信息】的业务，参数：{}", diagnosisReportUpdateParam);

        Long id = diagnosisReportUpdateParam.getId();
        DiagnosisReportStandardVO currentVO = diagnosisReportRepository.getStandardById(id);
        if (currentVO == null ) {
            String message = "修改诊断报告信息失败，尝试修改的报告数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        DiagnosisReport diagnosisReport=new DiagnosisReport();
        BeanUtils.copyProperties(diagnosisReportUpdateParam, diagnosisReport);

        int rows=diagnosisReportRepository.updateById(diagnosisReport);
        if (rows != 1) {
            String message = "修改诊断报告信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public DiagnosisReportStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询诊断报告】业务，参数：{}", id);
        DiagnosisReportStandardVO currentVO= diagnosisReportRepository.getStandardById(id);
        if(currentVO==null){
            String message="获取ID下诊断报告数据失败,尝试访问的诊断报告数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return currentVO;
    }

    @Override
    public PageData<DiagnosisReportListVO> listByCreateTime(Integer pageNum, String createTime) {
        log.debug("开始处理【根据创建时间查询诊断报告列表】业务，页码：{},预约创建时间:{}", pageNum,createTime);
        PageData<DiagnosisReportListVO> pageData = diagnosisReportRepository.listByCreateTime(pageNum, defaultQueryPageSize,createTime);
        return pageData;
    }

    @Override
    public PageData<DiagnosisReportListVO> listByCreateTime(Integer pageNum, Integer pageSize, String createTime) {
        log.debug("开始处理【根据创建时间查询诊断报告列表】业务，页码：{},报告创建时间:{}", pageNum,createTime);
        PageData<DiagnosisReportListVO> pageData = diagnosisReportRepository.listByCreateTime(pageNum, pageSize,createTime);
        return pageData;
    }

    @Override
    public PageData<DiagnosisReportListVO> listByPatientName(Integer pageNum, String patientName) {
        log.debug("开始处理【根据患者姓名查询诊断报告列表】业务，页码：{},患者姓名:{}", pageNum,patientName);
        PageData<DiagnosisReportListVO> pageData = diagnosisReportRepository.listByPatientName(pageNum, defaultQueryPageSize,patientName);
        return pageData;
    }

    @Override
    public PageData<DiagnosisReportListVO> listByPatientName(Integer pageNum, Integer pageSize, String patientName) {
        log.debug("开始处理【根据患者姓名查询预约单列表】业务，页码：{},患者姓名:{}", pageNum,patientName);
        PageData<DiagnosisReportListVO> pageData = diagnosisReportRepository.listByPatientName(pageNum, pageSize,patientName);
        return pageData;
    }

    @Override
    public PageData<DiagnosisReportListVO> list(Integer pageNum) {
        log.debug("开始处理【查询诊断报告列表】业务，页码：{}", pageNum);
        PageData<DiagnosisReportListVO> pageData = diagnosisReportRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DiagnosisReportListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询诊断报告列表】业务，页码：{}", pageNum);
        PageData<DiagnosisReportListVO> pageData = diagnosisReportRepository.list(pageNum, pageSize);
        return pageData;
    }
}
